﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LanusMarket.Models;

namespace LanusMarket.Controllers
{ 
    public class AvisoController : Controller
    {
        private LanusMarketContext db = new LanusMarketContext();

        //
        // GET: /Aviso/

        public ViewResult Index()
        {
            return View(db.Avisos.ToList());
        }

        //
        // GET: /Aviso/Details/5

        public ViewResult Details(int id)
        {
            Aviso aviso = db.Avisos.Find(id);
            return View(aviso);
        }

        //
        // GET: /Aviso/Create
        [Authorize]
        public ActionResult Create()
        {            
            ViewBag.CategoriaID = new SelectList(db.Categorias, "CategoriaID", "Nombre");
            return View();
        } 

        //
        // POST: /Aviso/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create(Aviso aviso)
        {
            if (ModelState.IsValid)
            {
                db.Avisos.Add(aviso);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            ViewBag.CategoriaID = new SelectList(db.Categorias, "CategoriaID", "Nombre",aviso.CategoriaID);
            return View(aviso);
        }
        
        //
        // GET: /Aviso/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            Aviso aviso = db.Avisos.Find(id);
            ViewBag.CategoriaID = new SelectList(db.Categorias, "CategoriaID", "Nombre",aviso.CategoriaID);
            return View(aviso);
        }

        //
        // POST: /Aviso/Edit/5
        [Authorize]
        [HttpPost]
        public ActionResult Edit(Aviso aviso)
        {
            if (ModelState.IsValid)
            {
                db.Entry(aviso).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoriaID = new SelectList(db.Categorias, "CategoriaID", "Nombre", aviso.CategoriaID);
            return View(aviso);
        }

        //
        // GET: /Aviso/Delete/5
        [Authorize]
        public ActionResult Delete(int id)
        {
            Aviso aviso = db.Avisos.Find(id);
            return View(aviso);
        }

        //
        // POST: /Aviso/Delete/5
        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Aviso aviso = db.Avisos.Find(id);
            db.Avisos.Remove(aviso);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}